/**
 * Created by Luxh on 2014/6/6.
 */



//保存数据
function doAdd(form_code){
    if(!dataForm.form.isValid()){
        Ext.Msg.alert('系统提示','请修正指出错误，检查输入是否正确!');
        return;
    }
    var params = dataForm.form.getValues();
    var _url = '/form/common/add?collection='+form_code;
    if(params && params._id ){
        _url = '/form/common/edit?collection='+form_code;
    }
    Ext.Ajax.request({
        url: _url,
        params : params,
        method : 'POST',
        success: function(response, opts) {
            var data = Ext.decode(response.responseText);
            if(data.flag == "success") {
                alert_msg_refresh_close_win('业务处理成功！');
            }else {
                alert_msg('业务处理失败！'+data.msg);
            }
        },
        failure: function(response, opts) {
            alert_msg('业务处理失败！');
        }
    });

}
//关闭窗口
function doClose(){
    parent.closeWin();
}


function getForm(form_code) {


    var tbar = [
        {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'},
        {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
    ];


    var dataForm = new Ext.FormPanel({
        layout : 'fit'
    });
    var items = [];
    var form = {};
    form.tbar = tbar;
    form.xtype = 'form';
    form.layout = {type: 'table',columns:3};
    Ext.Ajax.request({
        url: '/form/'+form_code,
        method : 'GET',
        success: function(response, opts) {
            var data = Ext.decode(response.responseText);
            if(data) {
                var field_arr = data.fields;
                var fields = [];
                fields.push({name :'_id',hidden : true});
                fields.push({name :'_max_file_size',hidden : true,value:100000});
                Ext.each(field_arr, function (f, index){
                    var params = [];
                    params.xtype = f.xtype;
                    params.name = f.name;
                    params.fieldLabel = f.field_label;
                    params.allowBlank = f.allow_blank;
                    params.colspan = 3;
                    if("radiogroup"==f.xtype) {
                        var opt_items = [];
                        var item = f.items;
                        var options = item.split('\n');

                        for(var i=0;i<options.length;i++) {
                            var option = options[i];

                            if(option!="") {
                                var json =eval("("+option+")");
                                opt_items.push(json);
                            }
                        }
                        params.columns = [100,100];
                        params.vertical = true;
                        params.items = opt_items;
                    }else if("checkboxgroup"==f.xtype){
                        var opt_items = [];
                        var item = f.items;
                        var options = item.split('\n');

                        for(var i=0;i<options.length;i++) {
                            var option = options[i];

                            if(option!="") {
                                var json =eval("("+option+")");
                                opt_items.push(json);
                            }
                        }
                        params.columns = [100,100];
                        params.vertical = true;
                        params.items = opt_items;
                    }else if("datefield"==f.xtype){
                        params.format = f.data_format;
                    }else if("filefield"==f.xtype){
                        var file_id = {};
                        file_id.xtype = 'textfield';
                        file_id.fieldLabel = '文件id';
                        file_id.name = 'image_file_id';
                        file_id.readOnly = true;
                        file_id.colspan = 1;
                        fields.push(file_id);
                        params.colspan = 1;
                        params.name = 'attach_file';
                    }else if("combobox"==f.xtype){
                        params.autoRender = true;
                        params.autoShow = true;
                        params.typeAhead = true;
                        params.queryMode = 'local';
                        params.editable = false;
                        params.displayField = f.display_field;
                        params.valueField = f.value_field;
                        if(!f.use_url) {

                            var store = {};
                            store.xtype = 'store';
                            var store_fields = [];
                            store_fields.push(f.display_field);
                            store_fields.push(f.value_field);
                            store.fields = store_fields;
                            var data = [];
                            var item = f.items;
                            var options = item.split('\n');
                            for(var i=0;i<options.length;i++) {
                                var option = options[i];
                                if(option!="") {
                                    var json =eval("("+option+")");
                                    data.push(json);
                                }
                            }
                            store.data = data;
                            params.store = store;

                        }else {
                            //远程数据源
                            var store = {};
                            store.xtype = 'store';
                            var store_fields = [];
                            store_fields.push(f.display_field);
                            store_fields.push(f.value_field);
                            store.fields = store_fields;
                            store.autoLoad = true;
                            var proxy = {
                                type: 'ajax',
                                url : f.url,
                                reader: {
                                    type: 'json',
                                    root: ''
                                }
                            };
                            store.proxy = proxy;
                            params.store = store;
                        }
                    }
                    fields.push(params);
                    if("filefield"==f.xtype) {
                        fields.push( {
                            xtype: "button",
                            text: "上传图片",
                            width: 100,
                            handler: function () {
                                uploadFile(dataForm);
                            },
                            style: 'margin-left: 10px;'
                        });
                    }
                });
                form.items = fields;
            }
            items.push(form);
            dataForm.add(items);
        },
        failure: function(response, opts) {

        }
    });
    return dataForm;
}


